#include "aTIM.h"

extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim4;


extern moreCoilData mMoreCoidData;
extern oneCoilData mOneCoidData1;
extern oneCoilData mOneCoidData2;
extern oneCoilData mOneCoidData3;
extern oneCoilData mOneCoidData4;
extern oneCoilData mOneCoidData5;
extern oneCoilData mOneCoidData6;
extern oneCoilData mOneCoidData7;
extern oneCoilData mOneCoidData8;

extern uint8_t ping_current_state[8];
extern uint8_t ping_save_state[8];

extern uint8_t min_lock_state;    //按下是0需要延时,否者不需要延时
extern uint8_t max_lock_state;    //上合是1

extern uint8_t open_dev_state;
extern uint8_t exec_pos;

extern uint32_t flash_valuse;

int tim2_exec_num;

void Ping_LED_exec(uint8_t num,uint8_t state);
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)  //回调
{
	if(htim->Instance == TIM2)   // 判断开关机
	{
		tim2_exec_num++;
		if(tim2_exec_num == 100)
		{
			tim2_exec_num = 0;
			if((max_lock_state == 1)&&(open_dev_state == 0))   			//关机切换到开机    当前还没有开机
			{
				lable:
				if((ping_save_state[exec_pos] == 1)&&(ping_save_state[exec_pos] != ping_current_state[exec_pos]))
				{
					Ping_LED_exec(exec_pos+1,1);
					if(exec_pos == 7)
					{
						open_dev_state = 1;
						HAL_TIM_Base_Stop_IT(&htim2);
					}
					exec_pos = exec_pos+1;
				}else
				{
					if(exec_pos == 7)
					{
						open_dev_state = 1;
						HAL_TIM_Base_Stop_IT(&htim2);
					}
					exec_pos = exec_pos+1;

					if(exec_pos < 8)
						goto lable;
				}
			}else if((max_lock_state == 0)&&(open_dev_state == 1))   	//开机切换到关机
			{
				lable1:
				if(ping_current_state[exec_pos] == 1)
				{
					Ping_LED_exec(exec_pos+1,0);
					if(exec_pos == 7)
					{
						open_dev_state = 0;
						HAL_TIM_Base_Stop_IT(&htim2);
					}
					exec_pos = exec_pos+1;
				}else
				{
					if(exec_pos == 7)
					{
						open_dev_state = 0;
						HAL_TIM_Base_Stop_IT(&htim2);
					}
					exec_pos = exec_pos+1;
					if(exec_pos < 8)
						goto lable1;
				}
			}
		}
	}else if(htim->Instance == TIM3)
	{
		if((max_lock_state == open_dev_state)&&(max_lock_state == 1)) //开机结束
		{
			if((min_lock_state == 1))    			//开机执行
			{
				if(mOneCoidData1.exec_state == 1)
				{
					Ping_LED_exec(1,mOneCoidData1.Coil_state);
					mOneCoidData1.exec_state = 0;
				}else if(mOneCoidData2.exec_state == 1)
				{
					Ping_LED_exec(2,mOneCoidData2.Coil_state);
					mOneCoidData2.exec_state = 0;
				}else if(mOneCoidData3.exec_state == 1)
				{
					Ping_LED_exec(3,mOneCoidData3.Coil_state);
					mOneCoidData3.exec_state = 0;
				}else if(mOneCoidData4.exec_state == 1)
				{
					Ping_LED_exec(4,mOneCoidData4.Coil_state);
					mOneCoidData4.exec_state = 0;
				}else if(mOneCoidData5.exec_state == 1)
				{
					Ping_LED_exec(5,mOneCoidData5.Coil_state);
					mOneCoidData5.exec_state = 0;
				}else if(mOneCoidData6.exec_state == 1)
				{
					Ping_LED_exec(6,mOneCoidData6.Coil_state);
					mOneCoidData6.exec_state = 0;
				}else if(mOneCoidData7.exec_state == 1)
				{
					Ping_LED_exec(7,mOneCoidData7.Coil_state);
					mOneCoidData7.exec_state = 0;
				}else if(mOneCoidData8.exec_state == 1)
				{
					Ping_LED_exec(8,mOneCoidData8.Coil_state);
					mOneCoidData8.exec_state = 0;
				}
			}else{
				if(mOneCoidData1.exec_state == 1)
				{
					mOneCoidData1.exec_time++;
					if(mOneCoidData1.exec_time == 100)
					{
						Ping_LED_exec(1,mOneCoidData1.Coil_state);
						mOneCoidData1.exec_state = 0;
					}
				}
				if(mOneCoidData2.exec_state == 1)
				{
					mOneCoidData2.exec_time++;
					if(mOneCoidData2.exec_time == 100)
					{
						Ping_LED_exec(2,mOneCoidData2.Coil_state);
						mOneCoidData2.exec_state = 0;
					}
				}
				if(mOneCoidData3.exec_state == 1)
				{
					mOneCoidData3.exec_time++;
					if(mOneCoidData3.exec_time == 100)
					{
						Ping_LED_exec(3,mOneCoidData3.Coil_state);
						mOneCoidData3.exec_state = 0;
					}
				}
				if(mOneCoidData4.exec_state == 1)
				{
					mOneCoidData4.exec_time++;
					if(mOneCoidData4.exec_time == 100)
					{
						Ping_LED_exec(4,mOneCoidData4.Coil_state);
						mOneCoidData4.exec_state = 0;
					}
				}
				if(mOneCoidData5.exec_state == 1)
				{
					mOneCoidData5.exec_time++;
					if(mOneCoidData5.exec_time == 100)
					{
						Ping_LED_exec(5,mOneCoidData5.Coil_state);
						mOneCoidData5.exec_state = 0;
					}
				}
				if(mOneCoidData6.exec_state == 1)
				{
					mOneCoidData6.exec_time++;
					if(mOneCoidData6.exec_time == 100)
					{
						Ping_LED_exec(6,mOneCoidData6.Coil_state);
						mOneCoidData6.exec_state = 0;
					}
				}
				if(mOneCoidData7.exec_state == 1)
				{
					mOneCoidData7.exec_time++;
					if(mOneCoidData7.exec_time == 100)
					{
						Ping_LED_exec(7,mOneCoidData7.Coil_state);
						mOneCoidData7.exec_state = 0;
					}
				}
				if(mOneCoidData8.exec_state == 1)
				{
					mOneCoidData8.exec_time++;
					if(mOneCoidData8.exec_time == 100)
					{
						Ping_LED_exec(8,mOneCoidData8.Coil_state);
						mOneCoidData8.exec_state = 0;
					}
				}
			}
		}
	}else if(htim->Instance == TIM4)
	{
		if((max_lock_state == open_dev_state)&&(max_lock_state == 1)) //开机结束
		{
			if((min_lock_state == 1))    			//开机执行没有延时
			{
				if(mMoreCoidData.exec_state == 1)
				{
					if(mMoreCoidData.Coil_num >= 1)
					{
						if(mMoreCoidData.CoilData[0] == 0)
						{
							Ping_LED_exec(1,0);
						}else{

							Ping_LED_exec(1,1);
						}
					}
					if(mMoreCoidData.Coil_num >= 2)
					{
						if(mMoreCoidData.CoilData[1] == 0)
						{
							Ping_LED_exec(2,0);
						}else{

							Ping_LED_exec(2,1);
						}
					}
					if(mMoreCoidData.Coil_num >= 3)
					{
						if(mMoreCoidData.CoilData[2] == 0)
						{
							Ping_LED_exec(3,0);
						}else{

							Ping_LED_exec(3,1);
						}
					}
					if(mMoreCoidData.Coil_num >= 4)
					{
						if(mMoreCoidData.CoilData[3] == 0)
						{
							Ping_LED_exec(4,0);
						}else{

							Ping_LED_exec(4,1);
						}
					}
					if(mMoreCoidData.Coil_num >= 5)
					{
						if(mMoreCoidData.CoilData[4] == 0)
						{
							Ping_LED_exec(5,0);
						}else{

							Ping_LED_exec(5,1);
						}
					}
					if(mMoreCoidData.Coil_num >= 6)
					{
						if(mMoreCoidData.CoilData[5] == 0)
						{
							Ping_LED_exec(6,0);
						}else{

							Ping_LED_exec(6,1);
						}
					}
					if(mMoreCoidData.Coil_num >= 7)
					{
						if(mMoreCoidData.CoilData[6] == 0)
						{
							Ping_LED_exec(7,0);
						}else{

							Ping_LED_exec(7,1);
						}
					}
					if(mMoreCoidData.Coil_num >= 8)
					{
						if(mMoreCoidData.CoilData[7] == 0)
						{
							Ping_LED_exec(8,0);
						}else{

							Ping_LED_exec(8,1);
						}
					}
					mMoreCoidData.exec_state = 0;
				}
			}else {                                 //有延时
				mMoreCoidData.exec_time = mMoreCoidData.exec_time+1;
				if(mMoreCoidData.exec_time == 100)
				{
					mMoreCoidData.exec_time = 0;
					lable3:
					if(mMoreCoidData.exec_state == 1)
					{
						mMoreCoidData.exec_current_pos = mMoreCoidData.exec_current_pos+1;
						if(mMoreCoidData.Coil_num >= mMoreCoidData.exec_current_pos)
						{
							uint8_t pos = mMoreCoidData.exec_current_pos;
							if(mMoreCoidData.CoilData[pos-1] == ping_current_state[pos-1])
							{
								goto lable3;
							}else{
								if(mMoreCoidData.CoilData[pos-1] == 0)
								{
									Ping_LED_exec(pos,0);
								}else{
									Ping_LED_exec(pos,1);
								}
							}
						}else{
							mMoreCoidData.exec_state = 0;
						}
					}
				}
			}//多线圈写有延时结束
		}//多线圈写结束
	}
}

void delay_us(uint16_t time)
{
	uint16_t begin = 0xffff-time-5;
	__HAL_TIM_SET_COUNTER(&htim3,begin);
	HAL_TIM_Base_Start(&htim3);
	while(begin < 0xffff-5)
	{
		begin = __HAL_TIM_GET_COUNTER(&htim3);
	}
	HAL_TIM_Base_Stop(&htim3);
}

void Ping_LED_exec(uint8_t num,uint8_t state)
{
	if(num == 1)
	{
		if(state == 0)
		{
			HAL_GPIO_WritePin(GPIOA,LED1_Pin, GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9, GPIO_PIN_RESET);


			ping_current_state[0] = 0;
		}else{
			HAL_GPIO_WritePin(GPIOA,LED1_Pin, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9, GPIO_PIN_SET);
			ping_current_state[0] = 1;
		}
	}else if(num == 2)
	{
		if(state == 0)
		{
			HAL_GPIO_WritePin(GPIOA,LED2_Pin, GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8, GPIO_PIN_RESET);
			ping_current_state[1] = 0;
		}else{
			HAL_GPIO_WritePin(GPIOA,LED2_Pin, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8, GPIO_PIN_SET);
			ping_current_state[1] = 1;
		}
	}else if(num == 3)
	{
		if(state == 0)
		{
			HAL_GPIO_WritePin(GPIOA,LED3_Pin, GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_RESET);
			ping_current_state[2] = 0;
		}else{
			HAL_GPIO_WritePin(GPIOA,LED3_Pin, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_SET);
			ping_current_state[2] = 1;
		}
	}else if(num == 4)
	{
		if(state == 0)
		{
			HAL_GPIO_WritePin(GPIOB,LED4_Pin,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_RESET);
			ping_current_state[3] = 0;
		}else{
			HAL_GPIO_WritePin(GPIOB,LED4_Pin,GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_SET);
			ping_current_state[3] = 1;
		}
	}else if(num == 5)
	{
		if(state == 0)
		{
			HAL_GPIO_WritePin(GPIOB,LED5_Pin,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_RESET);
			ping_current_state[4] = 0;
		}else{
			HAL_GPIO_WritePin(GPIOB,LED5_Pin,GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_SET);
			ping_current_state[4] = 1;
		}
	}else if(num == 6)
	{
		if(state == 0)
		{
			HAL_GPIO_WritePin(GPIOB, LED6_Pin,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,GPIO_PIN_RESET);
			ping_current_state[5] = 0;
		}else{
			HAL_GPIO_WritePin(GPIOB, LED6_Pin,GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,GPIO_PIN_SET);
			ping_current_state[5] = 1;
		}
	}else if(num == 7)
	{
		if(state == 0)
		{
			HAL_GPIO_WritePin(GPIOB, LED7_Pin,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_3,GPIO_PIN_RESET);
			ping_current_state[6] = 0;
		}else{
			HAL_GPIO_WritePin(GPIOB, LED7_Pin,GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOB,GPIO_PIN_3,GPIO_PIN_SET);
			ping_current_state[6] = 1;
		}
	}else if(num == 8)
	{
		if(state == 0)
		{
			HAL_GPIO_WritePin(GPIOA, LED8_Pin, GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_15, GPIO_PIN_RESET);
			ping_current_state[7] = 0;
		}else{
			HAL_GPIO_WritePin(GPIOA, LED8_Pin, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_15, GPIO_PIN_SET);
			ping_current_state[7] = 1;
		}
	}

	if(max_lock_state == 1)
	{
		ping_save_state[num - 1] = ping_current_state[num - 1];
		write_into_flash();
	}
}



void read_from_flash()
{
	HAL_FLASH_Unlock();
	flash_valuse = *(uint32_t *)0x0801FC00;

	uint8_t i = 0;
	for(i = 0;i < 8;i++)
	{
		ping_save_state[i] = (flash_valuse >> i)&(0x01);
	}
	HAL_FLASH_Lock();
}


void write_into_flash()
{
	uint32_t variable_write = 0;
	uint8_t i = 0;
	for(i = 0;i < 8;i++)
	{
		variable_write = variable_write << 1;
		variable_write = (variable_write)|(ping_save_state[i]);
	}
	uint32_t address = 0x0801FC00; // 选择一个在Flash中有效的地址
	HAL_FLASH_Unlock();
	FLASH_EraseInitTypeDef FlashSet;
	FlashSet.TypeErase = FLASH_TYPEERASE_PAGES;
	FlashSet.PageAddress = address;
	FlashSet.NbPages = 1;

	uint32_t PageError = 0;
	HAL_FLASHEx_Erase(&FlashSet, &PageError);

	HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, variable_write);

	HAL_FLASH_Lock();
}






